<!-- 为一个电商产品展示页面编写 HTML 和 CSS 代码，要求页面有一个轮播图区域（占据页面上半部分）和产品列表区域（每行展示 3 个产品图片及相关信息）。 -->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>电商产品展示</title>
    <style>
        * {
            box-sizing: border-box;
        }

        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
        }

        /* 轮播图容器 */
        .carousel {
            position: relative;
            width: 100%;
            min-height: 300px;
            overflow: hidden;
            background-color: #ccc;
        }

        /* 轮播图图片 */
        .carousel img {
            width: 100%;
            height: auto;
        }

        /* 控制按钮 */
        .carousel .controls {
            position: absolute;
            top: 50%;
            transform: translateY(-50%);
            z-index: 1;
        }

        .carousel .controls button {
            background-color: rgba(0, 0, 0, 0.5);
            color: white;
            border: none;
            padding: 10px;
            cursor: pointer;
        }

        .carousel .controls button:hover {
            background-color: rgba(0, 0, 0, 0.7);
        }

        /* 产品列表容器 */
        .product-list {
            display: flex;
            flex-wrap: wrap;
            justify-content: space-between;
            padding: 20px;
        }

        /* 单个产品容器 */
        .product {
            width: calc(30% - 20px);
            border: 1px solid #ddd;
            padding: 10px;
            margin-bottom: 20px;
        }

        .product img {
            width: 100%;
        }
    </style>
</head>

<body>
    <!-- 轮播图区域 -->
    <div class="carousel">
        <div id="carouselImages"></div>
        <div class="controls">
            <button id="prev">Prev</button>
            <button id="next">Next</button>
        </div>
    </div>
    <!-- 产品列表区域 -->
    <div class="product-list">
        <div class="product">
            <img src="case2-assets/product-1.jpeg" alt="Product 1" onerror="this.onerror=null; this.src='default-image.png';">
            <h3>Product 1</h3>
            <p>Description of Product 1</p>
        </div>
        <div class="product">
            <img src="case2-assets/product-2.jpeg" alt="Product 2" onerror="this.onerror=null; this.src='default-image.png';">
            <h3>Product 2</h3>
            <p>Description of Product 2</p>
        </div>
        <div class="product">
            <img src="case2-assets/product-3.jpeg" alt="Product 3" onerror="this.onerror=null; this.src='default-image.png';">
            <h3>Product 3</h3>
            <p>Description of Product 3</p>
        </div>
        <div class="product">
            <img src="case2-assets/product-4.jpeg" alt="Product 4" onerror="this.onerror=null; this.src='default-image.png';">
            <h3>Product 4</h3>
            <p>Description of Product 4</p>
        </div>
        <div class="product">
            <img src="case2-assets/product-5.jpeg" alt="Product 5" onerror="this.onerror=null; this.src='default-image.png';">
            <h3>Product 5</h3>
            <p>Description of Product 5</p>
        </div>
        <div class="product">
            <img src="case2-assets/product-1.jpeg" alt="Product 6" onerror="this.onerror=null; this.src='default-image.png';">
            <h3>Product 6</h3>
            <p>Description of Product 6</p>
        </div>
    </div>

    <script>
        // 获取轮播图元素
        const carousel = document.querySelector('.carousel');
        const imagesContainer = document.getElementById('carouselImages');
        const prevButton = document.getElementById('prev');
        const nextButton = document.getElementById('next');

        const imageUrls = [
            "case2-assets/product-1.jpeg",
            "case2-assets/product-2.jpeg",
            "case2-assets/product-3.jpeg"
        ];

        let currentIndex = 0;

        function createImages() {
            const fragment = document.createDocumentFragment();
            imageUrls.forEach(url => {
                const img = document.createElement('img');
                img.src = url;
                img.alt = `Carousel Image ${imageUrls.indexOf(url) + 1}`;
                img.onerror = () => {
                    img.onerror = null;
                    img.src = 'default-image.png';
                };
                fragment.appendChild(img);
            });
            imagesContainer.appendChild(fragment);
        }

        function showImage(index) {
            const images = imagesContainer.querySelectorAll('img');
            images.forEach((img, i) => {
                img.style.display = (i === index) ? 'block' : 'none';
            });
        }

        function nextImage() {
            try {
                currentIndex = (currentIndex + 1) % imageUrls.length;
                showImage(currentIndex);
            } catch (error) {
                console.error("Error in nextImage:", error);
            }
        }

        function prevImage() {
            try {
                currentIndex = (currentIndex - 1 + imageUrls.length) % imageUrls.length;
                showImage(currentIndex);
            } catch (error) {
                console.error("Error in prevImage:", error);
            }
        }

        // 初始化显示第一张图片
        createImages();
        showImage(currentIndex);

        // 绑定按钮事件
        prevButton.addEventListener('click', prevImage);
        nextButton.addEventListener('click', nextImage);

        // 自动播放
        let autoplayInterval = setInterval(nextImage, 3000);

        // 提供自动播放开关
        const toggleAutoplay = document.createElement('button');
        toggleAutoplay.textContent = 'Toggle Autoplay';
        toggleAutoplay.addEventListener('click', () => {
            if (autoplayInterval) {
                clearInterval(autoplayInterval);
                autoplayInterval = null;
                toggleAutoplay.textContent = 'Enable Autoplay';
            } else {
                autoplayInterval = setInterval(nextImage, 3000);
                toggleAutoplay.textContent = 'Disable Autoplay';
            }
        });

        document.body.appendChild(toggleAutoplay);
    </script>
</body>

</html>